package LeetCode.linkedlist;

import java.util.HashSet;
import java.util.Set;

public class RingListNode {
    int var;
    RingListNode next;

    public RingListNode(){

    }

    public RingListNode(int var){
        this.var = var;
        this.next=null;
    }

    public static RingListNode create(){
        RingListNode head = new RingListNode(0);
        RingListNode rld = head;
        RingListNode temp = null;
        for(int i=1;i<5;i++){
            rld.next = new RingListNode(i);
//            if(i==2){
//                temp = rld;
//            }
//           rld = rld.next;
        }
        rld.next = temp;

        return head;
    }

    public static boolean hasCycle(RingListNode head){
        Set<RingListNode> node = new HashSet<RingListNode>();
        while(head != null){
            if(node.contains(head)){
                return  true;
            }else {
                node.add(head);
            }
            head = head.next;
        }

        return false;
    }


    public static void main(String[] args) {
        RingListNode head = RingListNode.create();
        boolean res = RingListNode.hasCycle(head);
        System.out.println(res);
    }

}
